﻿Public Class AlertManager

    'Private alertedIds As List(Of String)
    Private _settings As UserSettings
    'Private _notify As NotifyIconManager

    Public Sub New(ByVal settings As UserSettings)
        _settings = settings


        BrowserManager.SetSettings(settings.Browser)
        SoundManager.SetSettings(settings.Sound)
        MailManager.SetSettings(settings.Mail)

        '_notify = New NotifyIconManager
        '_notify.ShowIcon()

    End Sub

    Public AlertedIds As New List(Of String)

    Private Sub OpenUri(ByVal state As Object)
        Dim program = DirectCast(state, LiveProgram)
        System.Threading.Thread.Sleep(_settings.Browser.Delay)
        BrowserManager.OpenUri(program)
    End Sub

    Private Sub Alert(ByVal program As LiveProgram, ByVal target As AlertTarget)
        program.IsAlerted = True

        ' Browser
        If target.AlertByBrowser Then
            'BrowserManager.OpenUri(program)
            Dim thread = New System.Threading.Thread(AddressOf OpenUri)
            thread.Start(program)
        End If

        If target.AlertByWindow Then
            ' TODO iconUriがNothingの可能性がある

            Dim limit = program.StartDateTime.AddMinutes(My.Settings.AirMinutes).Subtract(Now).Minutes
            Dim footer As String
            If limit < 0 Then
                Do While limit < 0
                    limit += My.Settings.AirMinutes
                Loop
                If limit > 28 Then
                    footer = "(延長？ 残り" & limit.ToString & "分)"
                Else
                    footer = "(延長 残り" & limit.ToString & "分)"
                End If
            Else
                footer = "(残り" & program.StartDateTime.AddMinutes(My.Settings.AirMinutes).Subtract(Now).Minutes.ToString & "分)"
            End If

            program.Station.NeedDisplay = True
            Dim window = New AlertWindow(New AlertWindowDataContext With { _
                                            .Title = program.Title, _
                                            .IconUri = program.Station.IconUri, _
                                            .Content = program.Content & vbCrLf & program.Station.Name, _
                                            .FooterLeft = If(program.IsMemberOnly, "限定", ""), _
                                            .FooterRight = footer, _
                                            .Uri = New Uri(program.Uri.ToString)})
            window.Show()
        End If

        ' Mail
        If target.AlertByMail Then
            MailManager.Send(program.Title, program.Uri.ToString & vbCrLf & program.Content & vbCrLf & program.Station.Name)
        End If

        ' Balloon
        If target.AlertByBalloon Then
            BalloonManager.ShowBalloon(program)
        End If

    End Sub

    Public Function Alert(ByVal programs As IList(Of LiveProgram)) As Boolean

        Dim isAlerted As Boolean
        Dim alertSound As Boolean

        ' 放送局
        For Each program In programs
            If program.IsAlerted Then
                Continue For
            End If

            ' 
            Dim sid = program.Station.Id
            Dim stations = From s In _settings.Stations Where s.Id = sid
            If stations.Count = 0 Then
                Continue For
            End If

            ' 重複通知暫定対応
            If AlertedIds.Contains(program.Id) Then
                Continue For
            Else
                AlertedIds.Add(program.Id)
                If AlertedIds.Count > 10000 Then
                    AlertedIds.RemoveAt(0)
                End If
            End If

            ' 通知
            Me.Alert(program, stations.First)
            isAlerted = True

            ' Sound
            If stations.First.AlertBySound Then
                alertSound = True
            End If
        Next

        ' キーワード
        'Dim keywords = From k In _settings.Keywords Where _
        '               k.Title.Trim <> "" OrElse _
        '               k.Content.Trim <> "" OrElse _
        '               k.Station.Trim <> "" OrElse _
        '               k.Caster.Trim <> ""

        If _settings.Keywords.Count > 0 Then
            For Each program In programs
                If program.IsAlerted Then
                    Continue For
                End If

                If AlertedIds.Contains(program.Id) Then ' *
                    Continue For
                End If

                For Each k In _settings.Keywords
                    If k.Title.Trim = "" AndAlso k.Content.Trim = "" AndAlso k.Station.Trim = "" AndAlso k.Caster.Trim = "" Then
                        Continue For
                    End If


                    If IsContains(program.Title, k.Title) AndAlso _
                       IsContains(program.Content, k.Content) AndAlso _
                       IsContains(program.Station.Name, k.Station) AndAlso _
                       IsContains(program.Caster, k.Caster) Then

                        ' 通知
                        Me.Alert(program, k)
                        AlertedIds.Add(program.Id) ' *
                        isAlerted = True

                        If k.AlertBySound Then
                            alertSound = True
                        End If
                        Exit For
                    End If
                Next
            Next
        End If


        If alertSound Then
            SoundManager.PlaySound()
        End If


        Return isAlerted


    End Function

    Private Shared Function IsContains(ByVal text As String, ByVal keyword As String) As Boolean
        If text Is Nothing Then
            Return False
        End If

        If keyword Is Nothing OrElse keyword.Trim = "" Then
            Return True
        End If

        Return text.ToLower.Trim.Contains(keyword.ToLower.Trim)

    End Function

End Class
